home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / sys5 / iscwmpst.z / iscwmpst / tcp / src / tcpout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-10  |  6.5 KB  |  228 lines

  1. /* @(#) $Header: tcpout.c,v 1.5 91/05/09 07:38:59 deyke Exp $ */
  2.  
  3. /* TCP output segment processing
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include "global.h"
  7. #include "timer.h"
  8. #include "mbuf.h"
  9. #include "netuser.h"
  10. #include "internet.h"
  11. #include "tcp.h"
  12. #include "ip.h"
  13.  
  14. static double mybackoff __ARGS((int n));
  15.  
  16. static double  mybackoff(n)
  17. int  n;
  18. {
  19.   double  b;
  20.  
  21.   for (b = 1.0; n > 0; b *= 1.25, n--) ;
  22.   return b;
  23. }
  24.  
  25. /* Send a segment on the specified connection. One gets sent only
  26.  * if there is data to be sent or if "force" is non zero
  27.  */
  28. void
  29. tcp_output(tcb)
  30. register struct tcb *tcb;
  31. {
  32.     struct pseudo_header ph;/* Pseudo-header for checksum calcs */
  33.     struct mbuf *hbp,*dbp;  /* Header and data buffer pointers */
  34.     int16 hsize;            /* Size of header */
  35.     struct tcp seg;         /* Local working copy of header */
  36.     int16 ssize;            /* Size of current segment being sent,
  37.                  * including SYN and FIN flags */
  38.     int16 dsize;            /* Size of segment less SYN and FIN */
  39.     int16 usable;           /* Usable window */
  40.     int16 sent;             /* Sequence count (incl SYN/FIN) already
  41.                  * in the pipe but not yet acked */
  42.  
  43.     if(tcb == NULLTCB)
  44.         return;
  45.  
  46.     switch(tcb->state){
  47.     case TCP_LISTEN:
  48.     case TCP_CLOSED:
  49.         return; /* Don't send anything */
  50.     }
  51.     for(;;){
  52.         /* Compute data already in flight */
  53.         sent = tcb->snd.ptr - tcb->snd.una;
  54.  
  55.         /* If transmitter has been idle for more than a RTT,
  56.          * take the congestion window back down to one packet.
  57.          */
  58.         if(!run_timer(&tcb->timer)
  59.          && (msclock() - tcb->lastactive) > tcb->srtt)
  60.             tcb->cwind = tcb->mss;
  61.  
  62.         /* Compute usable send window as minimum of offered
  63.          * and congestion windows, minus data already in flight.
  64.          * Be careful that the window hasn't shrunk --
  65.          * these are unsigned vars.
  66.          */
  67.         usable = min(tcb->snd.wnd,tcb->cwind);
  68.         if(usable > sent)
  69.             usable -= sent; /* Most common case */
  70.         else if(usable == 0 && sent == 0)
  71.             usable = 1;     /* Closed window probe */
  72.         else
  73.             usable = 0;     /* Window closed or shrunken */
  74.  
  75.         /* Compute size of segment we *could* send. This is the
  76.          * smallest of the usable window, the mss, or the amount
  77.          * we have on hand. (I don't like optimistic windows)
  78.          */
  79.         ssize = min(tcb->sndcnt - sent,usable);
  80.         ssize = min(ssize,tcb->mss);
  81.  
  82.         /* Now we decide if we actually want to send it.
  83.          * Apply John Nagle's "single outstanding segment" rule.
  84.          * If data is already in the pipeline, don't send
  85.          * more unless it is MSS-sized or the very last packet.
  86.          */
  87.         if(sent != 0 && ssize < tcb->mss
  88.          && !(tcb->state == TCP_FINWAIT1 && ssize == tcb->sndcnt-sent)){
  89.             ssize = 0;
  90.         }
  91.         /* Unless the tcp syndata option is on, inhibit data until
  92.          * our SYN has been acked. This ought to be OK, but some
  93.          * old TCPs have problems with data piggybacked on SYNs.
  94.          */
  95.         if(!tcb->flags.synack && !Tcp_syndata){
  96.             if(tcb->snd.ptr == tcb->iss)
  97.                 ssize = min(1,ssize);   /* Send only SYN */
  98.             else
  99.                 ssize = 0;      /* Don't send anything */
  100.         }
  101.         if(ssize == 0 && !tcb->flags.force)
  102.             break;          /* No need to send anything */
  103.  
  104.         tcb->flags.force = 0;   /* Only one forced segment! */
  105.  
  106.         seg.source = tcb->conn.local.port;
  107.         seg.dest = tcb->conn.remote.port;
  108.  
  109.         /* Set the flags according to the state we're in. It is
  110.          * assumed that if this segment is associated with a state
  111.          * transition, then the state change will already have been
  112.          * made. This allows this routine to be called from a
  113.          * retransmission timeout with force=1.
  114.          */
  115.         seg.flags.urg = 0; /* Not used in this implementation */
  116.         seg.flags.rst = 0;
  117.         seg.flags.ack = 1; /* Every state except TCP_SYN_SENT */
  118.         seg.flags.syn = 0; /* syn/fin/psh set later if needed */
  119.         seg.flags.fin = 0;
  120.         seg.flags.psh = 0;
  121.         seg.flags.congest = tcb->flags.congest;
  122.  
  123.         hsize = TCPLEN; /* Except when SYN being sent */
  124.         seg.mss = 0;
  125.         seg.optlen = 0;
  126.  
  127.         if(tcb->state == TCP_SYN_SENT)
  128.             seg.flags.ack = 0; /* Haven't seen anything yet */
  129.  
  130.         dsize = ssize;
  131.         if(!tcb->flags.synack && tcb->snd.ptr == tcb->iss){
  132.             /* Send SYN */
  133.             seg.flags.syn = 1;
  134.             dsize--;        /* SYN isn't really in snd queue */
  135.             /* Also send MSS */
  136.             seg.mss = Tcp_mss;
  137.             seg.optlen = 0;
  138.             hsize = TCPLEN + MSS_LENGTH;
  139.         }
  140.         seg.seq = tcb->snd.ptr;
  141.         seg.ack = tcb->rcv.nxt;
  142.         seg.wnd = tcb->rcv.wnd;
  143.         seg.up = 0;
  144.  
  145.         /* Now try to extract some data from the send queue. Since
  146.          * SYN and FIN occupy sequence space and are reflected in
  147.          * sndcnt but don't actually sit in the send queue, dup_p
  148.          * will return one less than dsize if a FIN needs to be sent.
  149.          */
  150.         if(dsize != 0){
  151.             int16 offset;
  152.  
  153.             /* SYN doesn't actually take up space on the sndq,
  154.              * so take it out of the sent count
  155.              */
  156.             offset = sent;
  157.             if(!tcb->flags.synack && sent != 0)
  158.                 offset--;
  159.  
  160.             if(dup_p(&dbp,tcb->sndq,offset,dsize) != dsize){
  161.                 /* We ran past the end of the send queue;
  162.                  * send a FIN
  163.                  */
  164.                 seg.flags.fin = 1;
  165.                 dsize--;
  166.             }
  167.         } else {
  168.             dbp = NULLBUF;
  169.         }
  170.         /* If the entire send queue will now be in the pipe, set the
  171.          * push flag
  172.          */
  173.         if(dsize != 0 && sent + ssize == tcb->sndcnt)
  174.             seg.flags.psh = 1;
  175.  
  176.         /* If this transmission includes previously transmitted data,
  177.          * snd.nxt will already be past snd.ptr. In this case,
  178.          * compute the amount of retransmitted data and keep score
  179.          */
  180.         if(tcb->snd.ptr < tcb->snd.nxt)
  181.             tcb->resent += min(tcb->snd.nxt - tcb->snd.ptr,ssize);
  182.  
  183.         tcb->snd.ptr += ssize;
  184.         /* If this is the first transmission of a range of sequence
  185.          * numbers, record it so we'll accept acknowledgments
  186.          * for it later
  187.          */
  188.         if(seq_gt(tcb->snd.ptr,tcb->snd.nxt))
  189.             tcb->snd.nxt = tcb->snd.ptr;
  190.  
  191.         /* Fill in fields of pseudo IP header */
  192.         ph.source = tcb->conn.local.address;
  193.         ph.dest = tcb->conn.remote.address;
  194.         ph.protocol = TCP_PTCL;
  195.         ph.length = hsize + dsize;
  196.  
  197.         /* Generate TCP header, compute checksum, and link in data */
  198.         if((hbp = htontcp(&seg,dbp,&ph)) == NULLBUF){
  199.             free_p(dbp);
  200.             return;
  201.         }
  202.         /* If we're sending some data or flags, start retransmission
  203.          * and round trip timers if they aren't already running.
  204.          */
  205.         if(ssize != 0){
  206.             /* Set round trip timer. */
  207.             set_timer(&tcb->timer,mybackoff(tcb->backoff)
  208.              * (4 * tcb->mdev + tcb->srtt));
  209.             if(!run_timer(&tcb->timer))
  210.                 start_timer(&tcb->timer);
  211.  
  212.             /* If round trip timer isn't running, start it */
  213.             if(!tcb->flags.rtt_run){
  214.                 tcb->flags.rtt_run = 1;
  215.                 tcb->rtt_time = msclock();
  216.                 tcb->rttseq = tcb->snd.ptr;
  217.             }
  218.         }
  219.         if(tcb->flags.retran)
  220.             tcpRetransSegs++;
  221.         else
  222.             tcpOutSegs++;
  223.  
  224.         ip_send(tcb->conn.local.address,tcb->conn.remote.address,
  225.          TCP_PTCL,tcb->tos,0,hbp,ph.length,0,0);
  226.     }
  227. }
  228.